﻿Public Class frmSach

    Private Sub Button3_Click(ByVal sender As System.Object, ByVal e As System.EventArgs)
        ListBox1.DisplayMember = "TenSach"
        ListBox1.ValueMember = "MaSach"
        Dim dt As DataTable
        dt = db.QueryTable("select*from Sach ")
        ListBox1.DataSource = dt



    End Sub

    Private Sub ListBox1_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ListBox1.SelectedIndexChanged
        'select TenTG from Sach_TacGia st inner join TacGia t on st.MaTG = t.MaTG where MaSach=''
        DatTacGia_Sach.DataSource = db.QueryTable("select TenTG,GhiChu from Sach_TacGia st inner join TacGia t on st.MaTG = t.MaTG where MaSach='" & ListBox1.SelectedValue & "'")
    End Sub

    Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
        Dim f As New frmSubSach
        f.TruyenThamSo(ListBox1.SelectedValue, ListBox1.Text)
        f.ShowDialog()
        DatTacGia_Sach.DataSource = db.QueryTable("select TenTG,GhiChu from Sach_TacGia st inner join TacGia t on st.MaTG = t.MaTG where MaSach='" & ListBox1.SelectedValue & "'")


    End Sub

    Private Sub frmSach_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
       
        ListBox1.DisplayMember = "TenSach"
        ListBox1.ValueMember = "MaSach"
        Dim dt As DataTable
        dt = db.QueryTable("select*from Sach ")
        ListBox1.DataSource = dt
        SetCbBox(cboPhanLoaiNXB, "TenNXB", "MaNXB", clsNhaXuatBan.DSNhaXuatBan)
        SetCbBox(cboPhanLoaiTL, "TenTL", "MaTL", ClsTheLoai.DSTheLoai)
        DatDauSach.DataSource = clsSach.DanhSachDS
        SetCbBox(cboTheLoai, "TenTL", "MaTL", ClsTheLoai.DSTheLoai)
        SetCbBox(cboNhaXuatBan, "TenNXB", "MaNXB", clsNhaXuatBan.DSNhaXuatBan)


    End Sub

    Private Sub cboPhanLoaiTL_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cboPhanLoaiTL.SelectedIndexChanged
        DatDauSach.DataSource = clsSach.PhanLoaiTL(cboPhanLoaiTL.SelectedValue)
    End Sub

    Private Sub cboPhanLoaiNXB_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cboPhanLoaiNXB.SelectedIndexChanged
        DatDauSach.DataSource = clsSach.PhanLoaiNXB(cboPhanLoaiNXB.SelectedValue)
    End Sub

    Private Sub btnReLoad_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnReLoad.Click
        DatDauSach.DataSource = clsSach.DanhSachDS
        txtTuKhoa.Text = ""
    End Sub
    Private Sub PhucHoiNutSua()
        If btnSua.Text = "Lưu lại" Then
            btnSua.Text = "Sửa"

        End If

    End Sub

    Private Sub btnThem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnThem.Click
        PhucHoiNutSua()
        clsSach.ThemS(txtMaSach.Text, txtTenSach.Text, cboTheLoai.SelectedValue, cboNhaXuatBan.SelectedValue, nbSoLuong.Value, txtViTri.Text, Val(txtNamXuatBan.Text))
        DatDauSach.DataSource = clsSach.DanhSachDS
        btnLamLai_Click(sender, e)

    End Sub

    Private Sub btnLamLai_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnLamLai.Click
        txtMaSach.Text = ""
        txtTenSach.Text = ""
        txtNamXuatBan.Text = ""
        txtViTri.Text = ""
        nbSoLuong.Value = 0
        'cboNhaXuatBan.Text = "Mặc định"
        'cboPhanLoaiTL.Text = "Mặc định"

    End Sub

    Private Sub btnPhanLouhai_Click(ByVal sender As System.Object, ByVal e As System.EventArgs)

    End Sub

    Private Sub btnSua_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnSua.Click
        If btnSua.Text = "Sửa" Then
            txtMaSach.Text = DatDauSach.CurrentRow.Cells("MaSach").Value
            txtTenSach.Text = DatDauSach.CurrentRow.Cells("TenSach").Value
            txtViTri.Text = DatDauSach.CurrentRow.Cells("ViTri").Value
            nbSoLuong.Value = DatDauSach.CurrentRow.Cells("SoLuong").Value
            txtNamXuatBan.Text = DatDauSach.CurrentRow.Cells("NamXB").Value
            cboTheLoai.Text = DatDauSach.CurrentRow.Cells("TenTL").Value '
            cboNhaXuatBan.Text = DatDauSach.CurrentRow.Cells("TenNXB").Value
            btnSua.Text = "Lưu lại"

        Else
            btnSua.Text = "Sửa"
            clsSach.SuaS(txtMaSach.Text, txtTenSach.Text, cboTheLoai.SelectedValue, cboNhaXuatBan.SelectedValue, nbSoLuong.Value, txtViTri.Text, Val(txtNamXuatBan.Text))
            DatDauSach.DataSource = clsSach.DanhSachDS
            btnLamLai_Click(sender, e)
        End If
    End Sub


    Private Sub txtNamXuatBan_KeyPress(ByVal sender As System.Object, ByVal e As System.Windows.Forms.KeyPressEventArgs) Handles txtNamXuatBan.KeyPress
        ChiNhapSo(sender, e)
    End Sub

    Private Sub btnTimKiem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnTimKiem.Click
        PhucHoiNutSua()
        DatDauSach.DataSource = clsSach.TimKiem(txtTuKhoa.Text)

    End Sub

    Private Sub btnXoa_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnXoa.Click
        PhucHoiNutSua()

        If MsgBox("Bạn có muốn xóa đầu sách có mã là " & txtMaSach.Text & " không ?", MsgBoxStyle.YesNo, "Thông báo") = DialogResult.Yes Then


            If clsSach.XoaS(txtMaSach.Text) = False Then
                MsgBox("Không thể xóa đầu sách có mã " & txtMaSach.Text & " vì chưa xóa hết các bản ghi liên quan.", MsgBoxStyle.Critical, "Lỗi")
            End If
            DatDauSach.DataSource = clsSach.DanhSachDS
        End If
    End Sub

    Private Sub DatDauSach_CellDoubleClick(ByVal sender As System.Object, ByVal e As System.Windows.Forms.DataGridViewCellEventArgs) Handles DatDauSach.CellDoubleClick
        PhucHoiNutSua()
        btnSua_Click(sender, e)
    End Sub

    Private Sub DatTacGia_Sach_CellContentClick(ByVal sender As System.Object, ByVal e As System.Windows.Forms.DataGridViewCellEventArgs) Handles DatTacGia_Sach.CellContentClick

    End Sub

    Private Sub BtnChiTiet_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles BtnChiTiet.Click
        Dim f As New frmChiTietSach
        f.ShowFromSach(DatDauSach.CurrentRow.Cells("MaSach").Value, DatDauSach.CurrentRow.Cells("TenSach").Value)

    End Sub
End Class